package cn.toleo.cart.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.toleo.common.pojo.ToleoResult;
import cn.toleo.pojo.TbItem;
import cn.toleo.service.ItemService;
import cn.toleo.utils.CookieUtils;
import cn.toleo.utils.JsonUtils;


/**
 * 购物车管理controller
 * @author Administrator
 *
 */
@Controller
public class CartController {
	//--------------------------------------------------------------------------------------------------------------------
	@Value("${CART_EXPIER}")
	private Integer CART_EXPIER;
	@Autowired
	private ItemService itemService;
	@RequestMapping("/cart/add/{itemId}")
	public String addItemCart(@PathVariable Long itemId,@RequestParam(defaultValue="1")Integer num,
			HttpServletRequest request,HttpServletResponse response){
		//主干基础
		List<TbItem> cartItemList= getCartItemList(request);
		boolean flag=false;
		//存在,原来数量+1
		for(TbItem tbItem:cartItemList){
			if(tbItem.getId()==itemId.longValue()){
				tbItem.setNum(tbItem.getNum()+num);
				flag=true;
				break;
			}
		}
		//原来不存在,添加新商品
		if(!flag){
			TbItem tbItem = itemService.getItemById(itemId);
			tbItem.setNum(num);
			String image = tbItem.getImage();
			if(StringUtils.isNotBlank(image)){
				String[] images = image.split(",");
				tbItem.setImage(images[0]);
			}
			cartItemList.add(tbItem);
		}
		CookieUtils.setCookie(request, response, CART_KEY, JsonUtils.objectToJson(cartItemList), CART_EXPIER, true);
		
		//结果
		return "cartSuccess";
	}
	
	
	@Value("${CART_KEY}")
	private String CART_KEY;
	private List<TbItem> getCartItemList(HttpServletRequest request){
		//主干基础
		String json = CookieUtils.getCookieValue(request, CART_KEY, true);
		if(StringUtils.isBlank(json)){
			//无值返回空的列表
			return new ArrayList<>();
		}
		List<TbItem> list = JsonUtils.jsonToList(json, TbItem.class);
		//结果
		return list;
	}
	
	//--------------------------------------------------------------------------------------------------------------------
	@RequestMapping("cart/cart")
	public String showCartList(HttpServletRequest request){
		//主干基础
		List<TbItem> cartItemList = getCartItemList(request);
		request.setAttribute("cartList", cartItemList);
		
		//结果
		return "cart";
	}
	
	//--------------------------------------------------------------------------------------------------------------------
	@RequestMapping("/cart/update/num/{itemId}/{num}")
	@ResponseBody
	public ToleoResult updateItemNum(@PathVariable Long itemId,@PathVariable Integer num,
			HttpServletRequest request,HttpServletResponse response){
		//主干基础
		List<TbItem> cartItemList = getCartItemList(request);
		for(TbItem tbItem:cartItemList){
			if(tbItem.getId()==itemId.longValue()){
				tbItem.setNum(num);
				break;
			}
		}
		//购物车列表写入cookie
		CookieUtils.setCookie(request, response,CART_KEY, JsonUtils.objectToJson(cartItemList),CART_EXPIER, true);		
		
		//结果
		return ToleoResult.ok();
	}
	
	//--------------------------------------------------------------------------------------------------------------------
	@RequestMapping("/cart/delete/{itemId}")
	public String deleteCartItem(@PathVariable Long itemId,HttpServletRequest request,HttpServletResponse response){
		//主干基础
		List<TbItem> cartItemList = getCartItemList(request);
		for(TbItem tbItem:cartItemList){
			if(tbItem.getId()==itemId.longValue()){
				cartItemList.remove(tbItem);
				break;
			}
		}
		CookieUtils.setCookie(request, response,CART_KEY, JsonUtils.objectToJson(cartItemList),CART_EXPIER, true);		
		
		//结果
		return "redirect:/cart/cart.html";
	}
}
